package com.lagou.rpc.provider;

import com.lagou.rpc.provider.server.RpcServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;

@SpringBootApplication
public class ServerBootstrapApplication implements CommandLineRunner, WebServerFactoryCustomizer<ConfigurableWebServerFactory> {


    @Autowired
    RpcServer rpcServer;

    public static void main(String[] args) {
        SpringApplication.run(ServerBootstrapApplication.class, args);
    }

    /**
     * 通过3个线程分别启动三个服务提供者，启动端口分别为8081，8082，8083
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        new Thread(() -> rpcServer.startServer("127.0.0.1", 8081)).start();
        new Thread(() -> rpcServer.startServer("127.0.0.1", 8082)).start();
        new Thread(() -> rpcServer.startServer("127.0.0.1", 8083)).start();
    }

    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(8080);
    }
}
